package com.by.sw;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@AutoConfiguration
@EnableConfigurationProperties(SwaggerPeoperties.class)
@ConditionalOnClass({Docket.class})
@EnableOpenApi //启动swagger注解支持
public class SwaggerAutoConfigure {

    SwaggerPeoperties swaggerPeoperties;

    public SwaggerAutoConfigure(SwaggerPeoperties swaggerPeoperties){
        this.swaggerPeoperties = swaggerPeoperties;
    }

    @Bean
    public Docket createDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .enable(swaggerPeoperties.getEnable())//为true可以访问 false不能访问
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerPeoperties.getBasepackage()))
                .paths(PathSelectors.ant(swaggerPeoperties.getMathPath()))
                .build();
    }

    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title(swaggerPeoperties.getTitle())
                .description(swaggerPeoperties.getDescrption())
                .contact(new Contact(swaggerPeoperties.getName(), swaggerPeoperties.getHost(), swaggerPeoperties.getEmail()))
                .version(swaggerPeoperties.getVersion())
                .build();
    }

}
